﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class fileAspx_transFile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpFileCollection files = HttpContext.Current.Request.Files;
        HttpPostedFile file = null;
        if (files.Count != 0)
        {
            file = files[0];
            string filename = file.FileName;
            string serverpath = Server.MapPath("../fileupload/"); //找到fileupload在服务器的位置 
            filename = DateTime.Now.ToString("yyyyMMddhhmmss") + filename;
            file.SaveAs(serverpath + filename); //把图片缓存到服务器上，压缩后，再删除 
            string strResizePicName = ResizePic(filename);
            //删除原图片
            System.IO.File.Delete(serverpath + filename);
            string json = "{\"code\": 0 ,\"msg\": \"\" ,\"data\": {\"src\": \" " + strResizePicName + "\"}}";
            Response.Write(json);
            Response.End();
        }
    }
    public string ResizePic(string fileName)
    {  //压缩图片开始
        bool IsImgFile = true; //判断是否为图片文件
        string fileSysName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg"; //修改后的文件名称
        string filePath = "";  //返回文件路径
        string strImgPath = "../fileupload/";  //上传到服务器的路径
        //压缩后的宽度和高度
        int w = 0;
        int h = 0;
        if (IsImgFile)
        {
            int maxWidth = 400;  //图片宽度最大限制
            int maxHeight = 300; //图片高度最大限制

            System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(strImgPath) + fileName);//创建一个图片对象
            int imgWidth = imgPhoto.Width;
            int imgHeight = imgPhoto.Height;  //原图片的高宽

            if (imgWidth > imgHeight) //如果宽度超过高度以宽度为准来压缩
            {
                if (imgWidth > maxWidth) //如果图片宽度超过限制
                {
                    float toImgWidth = maxWidth;  //图片压缩后的宽度
                    float toImgHeight = imgHeight / (float)(imgWidth / toImgWidth); //图片压缩后的高度
                    w = int.Parse(toImgWidth.ToString());
                    string x = toImgHeight.ToString();
                    string y;
                    if (x.IndexOf('.') > 0)
                        y = x.Substring(0, x.IndexOf('.'));//去掉后面的小数
                    else
                        y = x;
                    h = int.Parse(y);

                }
            }
            else
            {
                if (imgHeight > maxHeight)
                {
                    float toImgHeight1 = maxHeight;
                    float toImgWidth1 = imgWidth / (float)(imgHeight / toImgHeight1);
                    h = int.Parse(toImgHeight1.ToString());
                    string x = toImgWidth1.ToString();
                    string y;
                    if (x.IndexOf('.') > 0)
                        y = x.Substring(0, x.IndexOf('.'));//去掉后面的小数
                    else
                        y = x;
                    w = int.Parse(y);
                }
                else //原始文件不超过规定大小
                {
                    imgPhoto.Save(System.Web.HttpContext.Current.Server.MapPath(strImgPath) + fileSysName);
                    imgPhoto.Dispose();
                    return filePath;
                }
            }
            System.Drawing.Bitmap img = new System.Drawing.Bitmap(imgPhoto, w, h);
            int a = img.Width;
            string strResizePicName = System.Web.HttpContext.Current.Server.MapPath(strImgPath) + fileSysName;
            img.Save(strResizePicName); //保存压缩后的图片
            filePath = strImgPath + fileSysName; //返回压缩后的图片路径

            //释放资源
            img.Dispose();
            imgPhoto.Dispose();
            fileName.Clone();
            fileSysName.Clone();
        }

        return filePath;

    }
}